//*****************************************************************************
//
// Copyright, (C) 2000-2005 Quantic Dream SA
//
// These coded instructions, statements and computer programs contain
// unpublished information, proprietary to Quantic Dream SA and are 
// protected by French and EEC copyright laws. They may not be 
// disclosed to third parties or copied or duplicated, in whole or in part, 
// without prior written consent of Quantic Dream SA
//
// Unpublished-rights reserved under the Copyright Laws of the EEC.
//
//*****************************************************************************
//
//	CLASS:	CAMERA
//
//	JJA : 2012-07-23 - Created
//*****************************************************************************

#include	"Camera.h"

#define	DEFAULT_FIELD_OF_VIEW			45.0f
#define	DEFAULT_ASPECT_RATIO			1.7777777777777777777777777777778	// 16 / 9
#define	DEFAULT_NEAR_CLIIPING_DISTANCE	0.01f
#define	DEFAULT_FAR_CLIPPING_DISTANCE	10000.0f

//=============================================================================
//	CODE STARTS HERE
//=============================================================================

//-----------------------------------------------------------------------------
//! @brief		CAMERA constructor
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-07-23
//-----------------------------------------------------------------------------
CAMERA::CAMERA()
: _fFieldOfView				( DEFAULT_FIELD_OF_VIEW )
, _fAspectRatio				( DEFAULT_ASPECT_RATIO )
, _fNearClippingDistance	( DEFAULT_NEAR_CLIIPING_DISTANCE )
, _fFarClippingDistance		( DEFAULT_FAR_CLIPPING_DISTANCE )
, _vCameraPosition			( 0.0f, 0.0f, 10.0f )
, _vCameraAt				( -VECTOR3::zAxis() )
, _vCameraUp				( VECTOR3::yAxis() )
{
	UpdateProjectionMatrix();
	_mWorldMatrix = MATRIX4::lookAt( _vCameraPosition, _vCameraPosition + _vCameraAt, _vCameraUp );
}

//-----------------------------------------------------------------------------
//! @brief		CAMERA destructor
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-07-23
//-----------------------------------------------------------------------------
CAMERA::~CAMERA()
{
	
}

//-----------------------------------------------------------------------------
//! @brief		Translate
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-08-03
//------------------------------------------------------------------------------
void	CAMERA::Translate(const VECTOR3 &	vTranslate)
{
	VECTOR4 vTransformedCameraTranslation = orthoInverse( _mWorldMatrix ) * vTranslate;

	_vCameraPosition += vTransformedCameraTranslation.getXYZ();
	_mWorldMatrix = MATRIX4::lookAt( _vCameraPosition, _vCameraPosition + _vCameraAt, _vCameraUp );
}

//-----------------------------------------------------------------------------
//! @brief		RotateHead
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-08-03
//-----------------------------------------------------------------------------
void	CAMERA::RotateHead(const float	fAngleInRadians)
{
	MATRIX4 mHead = MATRIX4::rotation( fAngleInRadians, VECTOR3::yAxis() );
	VECTOR4 vTransformed;

	// Transform camera AT vector
	vTransformed	= mHead * _vCameraAt;
	_vCameraAt		= vTransformed.getXYZ();

	// Transform camera UP vector
	vTransformed	= mHead * _vCameraUp;
	_vCameraUp		= vTransformed.getXYZ();

	_mWorldMatrix = MATRIX4::lookAt( _vCameraPosition, _vCameraPosition + _vCameraAt, _vCameraUp );
}

//-----------------------------------------------------------------------------
//! @brief		RotatePitch
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-08-03
//-----------------------------------------------------------------------------
void	CAMERA::RotatePitch(const float	fAngleInRadians)
{
	MATRIX4 mPitch = MATRIX4::rotation( fAngleInRadians, orthoInverse( _mWorldMatrix ).getCol0().getXYZ() );
	VECTOR4 vTransformed;
	
	// Transform camera AT vector
	vTransformed	= mPitch * _vCameraAt;
	_vCameraAt		= vTransformed.getXYZ();

	// Transform camera UP vector
	vTransformed	= mPitch * _vCameraUp;
	_vCameraUp		= vTransformed.getXYZ();

	_mWorldMatrix = MATRIX4::lookAt( _vCameraPosition, _vCameraPosition + _vCameraAt, _vCameraUp );
}

//=============================================================================
//	CODE ENDS HERE
//=============================================================================
